/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Thread;

import ObjectFileOptions.ObjectFile;
import ObjectFileOptions.ObjectFileOption;
import Packets.pkValue;
import StaticClass.ArrSocket;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Oho
 */
public class ThreadChat extends Thread {

    ObjectInputStream in;
    ObjectOutputStream out;
    private String Command;
    private Socket sk;
    ServerSocket sv = null;
    int port;

    public ThreadChat(int port) throws IOException {
        this.port = port;


    }

    public ThreadChat(Socket sk) {

        this.sk = sk;

    }

    public ThreadChat(ObjectInputStream in, String command) {
        this.in = in;
        this.Command = command;
    }

    public ThreadChat() {
    }

    public void ThreadRead() {
        try {

            if (in == null) {
                in = new ObjectInputStream(sk.getInputStream());
            }
            ObjectFileOption ob = new ObjectFile(sk, in, out, this);
            ob.progessRead();
        } catch (IOException ex) {
            Logger.getLogger(ThreadChat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void ThreadWrite(pkValue pk) throws IOException {
        if (out == null) {
            out = new ObjectOutputStream(sk.getOutputStream());
        }

        ObjectFileOption ob = new ObjectFile(sk, in, out, this);
        ob.progessWrite(sk, pk);


    }

    @Override
    public void run() {

        ArrSocket.ArrChat.add(this);
        //sv = new ServerSocket(port);
        while (true) {
            
//            sk = sv.accept();
            ThreadRead();

            //sk = sv.accept();

            //os = client.getOutputStream();

        }


        //ThreadWrite();
        //if(vl.getCommand() ==0)

        //lenh chat
        //1 : lenh gui tin nhan 
        //2: lenh ....



    }

    /**
     * @return the Command
     */
    public String getCommand() {
        return Command;
    }

    /**
     * @param Command the Command to set
     */
    public void setCommand(String Command) {
        this.Command = Command;
    }

    /**
     * @return the sk
     */
    public Socket getSk() {
        return sk;
    }

    /**
     * @param sk the sk to set
     */
    public void setSk(Socket sk) {
        this.sk = sk;
    }
}
